<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *课目
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['id'] && $_RQ['op']=='list'){
	$this->session('video_id',$_RQ['id']);
}
if(!$this->session('video_id')){
	exi('请选择课程！','error');
}
$video=pdo_get('jy_xiaoeknow_video',array('id'=>$this->session('video_id'),'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']),array('title','type'));
$tabs=array(
	array('op'=>'list','title'=>$video['title'].'的课目列表'),
	array('op'=>'edit','title'=>'添加课目'),	
);
$return['xuanxiu']=$this->xuanxiu;
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'video_id'=>$this->session('video_id'),'merchid'=>$_SESSION['onemerchid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_xiaoeknow_video_jieitem',$params,array($pindex,$psize),$total,$fields,'','paixu desc,id desc');
	if($list){
		foreach($list as &$item){
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'排序越大越靠前！','edit'=>true,'list'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'id','title'=>'课目ID','type'=>'string'),
			array('name'=>'title','title'=>'课目名称','type'=>'string'),
			array('name'=>'readnum','title'=>'阅读量','type'=>'string'),
			array('name'=>'xuanxiu','title'=>'必修/选修','type'=>'radio','options'=>'xuanxiu'),
			array('name'=>'feetype','title'=>'收费类型','type'=>'radio','options'=>'types','show'=>array('showfeetype'=>1)),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'createtime','title'=>'发布时间','type'=>'time'),
		),'action' =>array(
			array('type'=>'post','op'=>'ok','show'=>array('status'=>array(0,2)),'title'=>'审核通过','message'=>'确定通过该课目吗！'),
			array('type'=>'post','op'=>'no','show'=>array('status'=>array(0,1)),'title'=>'不通过','message'=>'确定忽略该课目吗！'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'课目标题','type'=>'string'),
		)
	);
	if($video['type']){
		unset($htmls['list']['list'][5]);
	}
	$return['types']=$this->video_types;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_xiaoeknow_video_jieitem',array('id'=>$id,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']));		
	}else{
		$item=array('type'=>0);
	}
	$item['videos']=$item['videos']?json_decode($item['videos'],true):array();
	$item['images']=json_decode($item['images'],true)?:array();
	$item['showfeetype']=$video['type']?0:1;
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回课目列表'),
		),
		'edit'=>array(
				array('name'=>'paixu','title'=>'排序','type'=>'string'),
					array('name'=>'title','title'=>'课目名称','type'=>'string'),
					array('name'=>'image','title'=>'课目封面','type'=>'image'),
					array('name'=>'type','title'=>'课目类型','type'=>'radio','options'=>'content_types'),
					array('name'=>'xuanxiu','title'=>'必修/选修','type'=>'radio','options'=>'xuanxiu'),
					array('name'=>'videourl','title'=>'视频地址','type'=>'video','help'=>'（支持iframe... iframe格式）；也可以直接填远程网络地址，节省服务器压力，或系统设置远程附件（七牛，阿里云等，需要管理员操作！）'),
					array('name'=>'images','title'=>'图集','type'=>'images','show'=>array('type'=>2)),
					array('name'=>'desc','title'=>'课目介绍','type'=>'text'),
					array('type'=>'richtext','title'=>'课目详情','name'=>'content' , 'help'=>''),
					array('name'=>'feetype','title'=>'收费类型','type'=>'radio','options'=>'types','show'=>array('showfeetype'=>1)),
					array('name'=>'price','title'=>'课程价格','type'=>'string','show'=>array('feetype'=>1)),
					array('name'=>'readnum','title'=>'阅读人数','type'=>'string'),
					array('name'=>'times','title'=>'用时','type'=>'unitstring','unit'=>'小时'),
					array('name'=>'star','title'=>'难易星数','type'=>'radio','options'=>'star'),
					array('name'=>'showlevels','title'=>'商城会员等级浏览权限','type'=>'checkbox','options'=>'levels','help'=>'不填，表示不生效！'),
					array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$return['types']=$this->video_types;
	$levels=pdo_getall('jy_weishop_member_level',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels2']=$levels;
	$return['levels']=array();
	$return['levels'][0]='默认会员';
	foreach($levels as $lv){
		$return['levels'][$lv['id']]=$lv['title'];
	}
	$return['content_types']=$this->content_types;
	$return['star']=array(1=>'1星',2=>'2星',3=>'3星',4=>'4星',5=>'5星');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写课目名称！','error');
	}
	$data=array(
		'paixu'=>intval($postdata['paixu']),
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'type'=>intval($postdata['type']),
		'mid'=>intval($postdata['mid']),
		'desc'=>trim($postdata['desc']),
		'videourl'=>trim($postdata['videourl']),
		'content'=>trim($postdata['content']),
		'readnum'=>intval($postdata['readnum']),
		'status'=>intval($postdata['status']),
		'star'=>intval($postdata['star']),
		'times'=>intval($postdata['times']),
		'images'=>ijson_encode($postdata['images']),
		'xuanxiu'=>intval($postdata['xuanxiu']),
		'showlevels'=>trim($postdata['showlevels']),
		'feetype'=>intval($post['feetype']),
		'price'=>trim($post['price']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_xiaoeknow_video_jieitem',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$_SESSION['onemerchid'];
		$data['video_id']=$this->session('video_id');
		$data['createtime']=TIMESTAMP;
		$data['updatetime']=TIMESTAMP;
		pdo_insert('jy_xiaoeknow_video_jieitem',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择课目！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_xiaoeknow_video_jieitem',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='no'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要忽略的课目！','error');
	}
	pdo_update('jy_xiaoeknow_video_jieitem',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi('已忽略该课目！');
}
if($_RQ['op']=='ok'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要审核的课目！','error');
	}
	pdo_update('jy_xiaoeknow_video_jieitem',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi('已审核通过该课目！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_xiaoeknow_video_jieitem',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}